package com.cnpinxi.controller.admin.distributionRate;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.DistributionRate;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.DistributionRateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/distributionRate")
@Controller
public class AdminDistributionRateController extends BaseController{
    @Autowired
    DistributionRateService distributionRateService;

    /**
     * 查询优惠配置列表
     * @param request
     * @return
     */
    @RequestMapping("/distributionRateList")
    public ModelAndView distributionRateList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/distributionRate/distributionRateList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<DistributionRate> distributionRategList = distributionRateService.getDistributionRateList(map);
        PageInfo<DistributionRate> pageinfo = new PageInfo<>(distributionRategList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean",map);
        return m;
    }

    /**
     * 跳转到优惠配置编辑或添加页面
     * @param request
     * @return
     */
    @RequestMapping("/getDistributionRate")
    public ModelAndView getDistributionRate(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/admin/distributionRate/distributionRate");
        Map<String, String> map = this.showRequestParameters(request);
        String tid=map.get("tid");
        if(StringUtils.isNotBlank(tid)){
            DistributionRate distributionRate = distributionRateService.getDistributionRate(map);
            m.addObject("distributionRate",distributionRate);
        }
        return m;
    }

    /**
     * 添加或修改优惠配置
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/modifiyDistributionRate")
    @ResponseBody
    public String modifiyDistributionRate(HttpServletRequest request, DistributionRate distributionRate) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        Map<String, String> map = this.showRequestParameters(request);
        logger.info(distributionRate.toString());
        try {
            distributionRateService.modifyDistributionRate(distributionRate);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delDistributionRateById")
    @ResponseBody
    public String delDistributionRateById(HttpServletRequest request, DistributionRate distributionRate) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        Map<String, String> map = this.showRequestParameters(request);
        logger.info(distributionRate.toString());
        try {
            distributionRate.setTid(Integer.parseInt(map.get("tid")));
            distributionRateService.deleteDistributionRate(distributionRate);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg("操作失败！");
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
